USE [NGroup.VTL.DB]
GO

/****** Object:  StoredProcedure [dbo].[AddNewUser]    Script Date: 12/27/2013 12:43:42 AM ******/
DROP PROCEDURE [dbo].[AddNewUser]
GO

/****** Object:  StoredProcedure [dbo].[AddNewUser]    Script Date: 12/27/2013 12:43:42 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Bui Thanh Nam
-- Create date: 20131223
-- Description:	Add new user
-- =============================================
CREATE PROCEDURE [dbo].[AddNewUser] 
	@RoleID				INT,
	@Username			NVARCHAR(50),
	@Password			NVARCHAR(200),
	@UserAlias			NVARCHAR(100) = NULL,
	@IsActive			BIT = 1,
	@CreatedAt			DATETIME = NULL,
	@CreatedBy			NVARCHAR(50) = NULL,
	@LastUpdatedAt		DATETIME = NULL,
	@LastUpdatedBy		NVARCHAR(50) = NULL,
	@NewIdentity			INT = NULL OUTPUT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	IF NOT EXISTS(SELECT * FROM [dbo].[Users] WITH (NOLOCK) WHERE [Username] = @Username)
	BEGIN
    
		INSERT INTO [dbo].[Users]
			   ([RoleID]
			   ,[Username]
			   ,[Password]
			   ,[UserAlias]
			   ,[IsActive]
			   ,[CreatedAt]
			   ,[CreatedBy]
			   ,[LastUpdatedAt]
			   ,[LastUpdatedBy])
		 VALUES
			   (@RoleID
			   ,@Username
			   ,@Password
			   ,@UserAlias
			   ,@IsActive
			   ,@CreatedAt
			   ,@CreatedBy
			   ,@LastUpdatedAt
			   ,@LastUpdatedBy)
	END

	SELECT @NewIdentity = @@IDENTITY

END

GO


